Imports System.Data
Imports System.Data.SqlClient
Imports Conexao_liion

Partial Class Fornecedor
    Inherits System.Web.UI.Page
    Dim blnGridSelectedRow As Boolean = False

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        'If Not Session("Logged") Then
        '    Response.Redirect("Login.aspx")
        'End If

        Response.Cache.SetCacheability(HttpCacheability.ServerAndNoCache)

        ' Header
        lblModulo.Text = "Administrador"
        lblModuloItem.Text = "Cadastro de Fornecedor de Material"

        btnIncluir.Enabled = False
        btnAlterar.Enabled = False
        btnExcluir.Enabled = False
        btnDadosFiscais.Enabled = False

        If (CarregaGVW() = True) Then
            btnIncluir.Enabled = True
        End If

    End Sub


    Private Function CarregaGVW() As Boolean

        CarregaGVW = False

        Using theSqlConnection As New SqlConnection(Conexao_liion.theConnectionString)

            Dim mydatatable As New DataTable
            Dim theQueryString As String

            theQueryString = "SELECT A.CodFornecedor, A.RazaoSocialFornecedor, B.DscTipoPessoa, A.CNPJCPFFornecedor, C.Sigla2DoPais " & _
                    "FROM Fornecedor A, tipopessoa B, pais C " & _
                    "WHERE A.CodTipoPessoa *= B.CodTipoPessoa AND A.CodigoDoPaisISO *= C.CodigoDoPaisISO " & _
                    "ORDER BY A.RazaoSocialFornecedor;"


            Dim theSqlCommand As SqlCommand = theSqlConnection.CreateCommand()
            theSqlCommand.CommandText = theQueryString

            Try
                theSqlConnection.Open()
                Dim theSqlDataReader As SqlDataReader = theSqlCommand.ExecuteReader()

                mydatatable.Load(theSqlDataReader)
                gvwData.DataSource = mydatatable
                gvwData.DataBind()

                CarregaGVW = True
                theSqlDataReader.Close()

            Catch ex As Exception
                'theSqlDataReader.Close()
                lblMsg.Text = ex.Message

            End Try

        End Using

    End Function

    Protected Sub gvwData_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles gvwData.PageIndexChanging

        Try
            gvwData.PageIndex = e.NewPageIndex
            gvwData.DataBind()
        Catch ex As Exception
            lblMsg.Text = ex.Message
        End Try

    End Sub

    Protected Sub gvwData_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles gvwData.SelectedIndexChanged

        'If Not Page.IsPostBack Then
        btnAlterar.Enabled = True
        btnExcluir.Enabled = True
        btnDadosFiscais.Enabled = True
        'End If

    End Sub



    Protected Sub btnIncluir_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnIncluir.Click

        Session.Add("strOperacao", "I") ' Incluir

        Session.Add("strCodFornecedor", "")
        Session.Add("strRazaoSocialFornecedor", "")
        Session.Add("strDscTipoPessoa", "")

        Response.Redirect("FornecedorData.aspx")

    End Sub

    Protected Sub btnAlterar_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAlterar.Click

        Session.Add("strOperacao", "A") ' Alterar

        Session.Add("strCodFornecedor", HttpUtility.HtmlDecode(gvwData.SelectedRow.Cells(0).Text))
        Session.Add("strRazaoSocialFornecedor", HttpUtility.HtmlDecode(gvwData.SelectedRow.Cells(1).Text))
        Session.Add("strDscTipoPessoa", HttpUtility.HtmlDecode(gvwData.SelectedRow.Cells(2).Text))

        Response.Redirect("FornecedorData.aspx")

    End Sub

    Protected Sub btnExcluir_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnExcluir.Click

        Session.Add("strOperacao", "E") ' Excluir

        Session.Add("strCodFornecedor", HttpUtility.HtmlDecode(gvwData.SelectedRow.Cells(0).Text))
        Session.Add("strRazaoSocialFornecedor", HttpUtility.HtmlDecode(gvwData.SelectedRow.Cells(1).Text))
        Session.Add("strDscTipoPessoa", HttpUtility.HtmlDecode(gvwData.SelectedRow.Cells(2).Text))

        Response.Redirect("FornecedorData.aspx")


    End Sub

    Protected Sub btnVoltar_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnVoltar.Click

        Response.Redirect("Default.aspx")

    End Sub

    Protected Sub btnDadosFiscais_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnDadosFiscais.Click

        Session.Add("strOperacao", "A") ' Alterar

        Session.Add("strCodFornecedor", HttpUtility.HtmlDecode(gvwData.SelectedRow.Cells(0).Text))
        Session.Add("strRazaoSocialFornecedor", HttpUtility.HtmlDecode(gvwData.SelectedRow.Cells(1).Text))
        Session.Add("strDscTipoPessoa", HttpUtility.HtmlDecode(gvwData.SelectedRow.Cells(2).Text))

        Response.Redirect("FornecedorDadosFiscais.aspx")

    End Sub

End Class
